documentation
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 if ( ! defined( 'MEDIAWIKI' ) )
3 die( -1 );
4 /**
5 * @package MediaWiki
6 */
7
8 if ( $wgUseTeX )
9 require_once 'Math.php';
10
11 /**
12 * @todo document
13 * @package MediaWiki
14 */
15 class OutputPage {
16 var $mHeaders, $mMetatags, $mKeywords;
17 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
18 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
19 var $mSubtitle, $mRedirect, $mStatusCode;
20 var $mLastModified, $mETag, $mCategoryLinks;
21 var $mScripts, $mLinkColours;
22
23 var $mSuppressQuickbar;
24 var $mOnloadHandler;
25 var $mDoNothing;
26 var $mContainsOldMagic, $mContainsNewMagic;
27 var $mIsArticleRelated;
28 var $mParserOptions;
29 var $mShowFeedLinks = false;
30 var $mEnableClientCache = true;
31 var $mArticleBodyOnly = false;
32
33 /**
34 * Constructor
35 * Initialise private variables
36 */
37 function OutputPage() {
38 $this->mHeaders = $this->mMetatags =
39 $this->mKeywords = $this->mLinktags = array();
40 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
41 $this->mRedirect = $this->mLastModified =
42 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
43 $this->mOnloadHandler = '';
44 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
45 $this->mSuppressQuickbar = $this->mPrintable = false;
46 $this->mLanguageLinks = array();
47 $this->mCategoryLinks = array() ;
48 $this->mDoNothing = false;
49 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
50 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
51 $this->mSquidMaxage = 0;
52 $this->mScripts = '';
53 $this->mETag = false;
54 $this->mRevisionId = null;
55 }
56
57 function addHeader( $name, $val ) { array_push( $this->mHeaders, $name.': '.$val ) ; }
58 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
59 function setStatusCode( $statusCode ) { $this->mStatusCode = $statusCode; }
60
61 # To add an http-equiv meta tag, precede the name with "http:"
62 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
63 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
64 function addScript( $script ) { $this->mScripts .= $script; }
65 function getScript() { return $this->mScripts; }
66
67 function setETag($tag) { $this->mETag = $tag; }
68 function setArticleBodyOnly($only) { $this->mArticleBodyOnly = $only; }
69 function getArticleBodyOnly($only) { return $this->mArticleBodyOnly; }
70
71 function addLink( $linkarr ) {
72 # $linkarr should be an associative array of attributes. We'll escape on output.
73 array_push( $this->mLinktags, $linkarr );
74 }
75
76 function addMetadataLink( $linkarr ) {
77 # note: buggy CC software only reads first "meta" link
78 static $haveMeta = false;
79 $linkarr['rel'] = ($haveMeta) ? 'alternate meta' : 'meta';
80 $this->addLink( $linkarr );
81 $haveMeta = true;
82 }
83
84 /**
85 * checkLastModified tells the client to use the client-cached page if
86 * possible. If sucessful, the OutputPage is disabled so that
87 * any future call to OutputPage->output() have no effect. The method
88 * returns true iff cache-ok headers was sent.
89 */
90 function checkLastModified ( $timestamp ) {
91 global $wgCachePages, $wgCacheEpoch, $wgUser;
92 if ( !$timestamp || $timestamp == '19700101000000' ) {
93 wfDebug( "CACHE DISABLED, NO TIMESTAMP\n" );
94 return;
95 }
96 if( !$wgCachePages ) {
97 wfDebug( "CACHE DISABLED\n", false );
98 return;
99 }
100 if( $wgUser->getOption( 'nocache' ) ) {
101 wfDebug( "USER DISABLED CACHE\n", false );
102 return;
103 }
104
105 $timestamp=wfTimestamp(TS_MW,$timestamp);
106 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched, $wgCacheEpoch ) );
107
108 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
109 # IE sends sizes after the date like this:
110 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
111 # this breaks strtotime().
112 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
113 $modsinceTime = strtotime( $modsince );
114 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
115 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
116 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
117 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
118 # Make sure you're in a place you can leave when you call us!
119 header( "HTTP/1.0 304 Not Modified" );
120 $this->mLastModified = $lastmod;
121 $this->sendCacheControl();
122 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
123 $this->disable();
124 @ob_end_clean(); // Don't output compressed blob
125 return true;
126 } else {
127 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
128 $this->mLastModified = $lastmod;
129 }
130 } else {
131 wfDebug( "client did not send If-Modified-Since header\n", false );
132 $this->mLastModified = $lastmod;
133 }
134 }
135
136 function getPageTitleActionText () {
137 global $action;
138 switch($action) {
139 case 'edit':
140 case 'delete':
141 case 'protect':
142 case 'unprotect':
143 case 'watch':
144 case 'unwatch':
145 // Display title is already customized
146 return '';
147 case 'history':
148 return wfMsg('history_short');
149 case 'submit':
150 // FIXME: bug 2735; not correct for special pages etc
151 return wfMsg('preview');
152 case 'info':
153 return wfMsg('info_short');
154 default:
155 return '';
156 }
157 }
158
159 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
160 function setHTMLTitle( $name ) {$this->mHTMLtitle = $name; }
161 function setPageTitle( $name ) {
162 global $action, $wgContLang;
163 $name = $wgContLang->convert($name, true);
164 $this->mPagetitle = $name;
165 if(!empty($action)) {
166 $taction = $this->getPageTitleActionText();
167 if( !empty( $taction ) ) {
168 $name .= ' - '.$taction;
169 }
170 }
171
172 $this->setHTMLTitle( wfMsg( 'pagetitle', $name ) );
173 }
174 function getHTMLTitle() { return $this->mHTMLtitle; }
175 function getPageTitle() { return $this->mPagetitle; }
176 function setSubtitle( $str ) { $this->mSubtitle = /*$this->parse(*/$str/*)*/; } // @bug 2514
177 function getSubtitle() { return $this->mSubtitle; }
178 function isArticle() { return $this->mIsarticle; }
179 function setPrintable() { $this->mPrintable = true; }
180 function isPrintable() { return $this->mPrintable; }
181 function setSyndicated( $show = true ) { $this->mShowFeedLinks = $show; }
182 function isSyndicated() { return $this->mShowFeedLinks; }
183 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
184 function getOnloadHandler() { return $this->mOnloadHandler; }
185 function disable() { $this->mDoNothing = true; }
186
187 function setArticleRelated( $v ) {
188 $this->mIsArticleRelated = $v;
189 if ( !$v ) {
190 $this->mIsarticle = false;
191 }
192 }
193 function setArticleFlag( $v ) {
194 $this->mIsarticle = $v;
195 if ( $v ) {
196 $this->mIsArticleRelated = $v;
197 }
198 }
199
200 function isArticleRelated() { return $this->mIsArticleRelated; }
201
202 function getLanguageLinks() { return $this->mLanguageLinks; }
203 function addLanguageLinks($newLinkArray) {
204 $this->mLanguageLinks += $newLinkArray;
205 }
206 function setLanguageLinks($newLinkArray) {
207 $this->mLanguageLinks = $newLinkArray;
208 }
209
210 function getCategoryLinks() {
211 return $this->mCategoryLinks;
212 }
213
214 /**
215 * Add an array of categories, with names in the keys
216 */
217 function addCategoryLinks($categories) {
218 global $wgUser, $wgContLang;
219
220 # Add the links to the link cache in a batch
221 $arr = array( NS_CATEGORY => $categories );
222 $lb = new LinkBatch;
223 $lb->setArray( $arr );
224 $lb->execute();
225
226 $sk =& $wgUser->getSkin();
227 foreach ( $categories as $category => $arbitrary ) {
228 $title = Title::makeTitleSafe( NS_CATEGORY, $category );
229 $text = $wgContLang->convertHtml( $title->getText() );
230 $this->mCategoryLinks[] = $sk->makeLinkObj( $title, $text );
231 }
232 }
233
234 function setCategoryLinks($categories) {
235 $this->mCategoryLinks = array();
236 $this->addCategoryLinks($categories);
237 }
238
239 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
240 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
241
242 function addHTML( $text ) { $this->mBodytext .= $text; }
243 function clearHTML() { $this->mBodytext = ''; }
244 function getHTML() { return $this->mBodytext; }
245 function debug( $text ) { $this->mDebugtext .= $text; }
246
247 /* @deprecated */
248 function setParserOptions( $options ) {
249 return $this->ParserOptions( $options );
250 }
251
252 function ParserOptions( $options = null ) {
253 return wfSetVar( $this->mParserOptions, $options );
254 }
255
256 /**
257 * Set the revision ID which will be seen by the wiki text parser
258 * for things such as embedded {{REVISIONID}} variable use.
259 * @param mixed $revid an integer, or NULL
260 * @return mixed previous value
261 */
262 function setRevisionId( $revid ) {
263 $val = is_null( $revid ) ? null : intval( $revid );
264 return wfSetVar( $this->mRevisionId, $val );
265 }
266
267 /**
268 * Convert wikitext to HTML and add it to the buffer
269 * Default assumes that the current page title will
270 * be used.
271 */
272 function addWikiText( $text, $linestart = true ) {
273 global $wgTitle;
274 $this->addWikiTextTitle($text, $wgTitle, $linestart);
275 }
276
277 function addWikiTextWithTitle($text, &$title, $linestart = true) {
278 $this->addWikiTextTitle($text, $title, $linestart);
279 }
280
281 function addWikiTextTitle($text, &$title, $linestart) {
282 global $wgParser;
283 $parserOutput = $wgParser->parse( $text, $title, $this->mParserOptions,
284 $linestart, true, $this->mRevisionId );
285 $this->addParserOutput( $parserOutput );
286 }
287
288 function addParserOutputNoText( &$parserOutput ) {
289 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
290 $this->addCategoryLinks( $parserOutput->getCategories() );
291 $this->addKeywords( $parserOutput );
292 if ( $parserOutput->getCacheTime() == -1 ) {
293 $this->enableClientCache( false );
294 }
295 }
296
297 function addParserOutput( &$parserOutput ) {
298 $this->addParserOutputNoText( $parserOutput );
299 $this->addHTML( $parserOutput->getText() );
300 }
301
302 /**
303 * Add wikitext to the buffer, assuming that this is the primary text for a page view
304 * Saves the text into the parser cache if possible
305 */
306 function addPrimaryWikiText( $text, $article, $cache = true ) {
307 global $wgParser, $wgUser;
308
309 $this->mParserOptions->setTidy(true);
310 $parserOutput = $wgParser->parse( $text, $article->mTitle,
311 $this->mParserOptions, true, true, $this->mRevisionId );
312 $this->mParserOptions->setTidy(false);
313 if ( $cache && $article && $parserOutput->getCacheTime() != -1 ) {
314 $parserCache =& ParserCache::singleton();
315 $parserCache->save( $parserOutput, $article, $wgUser );
316 }
317
318 $this->addParserOutputNoText( $parserOutput );
319 $text = $parserOutput->getText();
320 wfRunHooks( 'OutputPageBeforeHTML',array( &$this, &$text ) );
321 $parserOutput->setText( $text );
322 $this->addHTML( $parserOutput->getText() );
323 }
324
325 /**
326 * For anything that isn't primary text or interface message
327 */
328 function addSecondaryWikiText( $text, $linestart = true ) {
329 global $wgTitle;
330 $this->mParserOptions->setTidy(true);
331 $this->addWikiTextTitle($text, $wgTitle, $linestart);
332 $this->mParserOptions->setTidy(false);
333 }
334
335
336 /**
337 * Add the output of a QuickTemplate to the output buffer
338 * @param QuickTemplate $template
339 */
340 function addTemplate( &$template ) {
341 ob_start();
342 $template->execute();
343 $this->addHTML( ob_get_contents() );
344 ob_end_clean();
345 }
346
347 /**
348 * Parse wikitext and return the HTML. This is for special pages that add the text later
349 */
350 function parse( $text, $linestart = true ) {
351 global $wgParser, $wgTitle;
352 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions,
353 $linestart, true, $this->mRevisionId );
354 return $parserOutput->getText();
355 }
356
357 /**
358 * @param $article
359 * @param $user
360 *
361 * @return bool
362 */
363 function tryParserCache( &$article, $user ) {
364 $parserCache =& ParserCache::singleton();
365 $parserOutput = $parserCache->get( $article, $user );
366 if ( $parserOutput !== false ) {
367 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
368 $this->addCategoryLinks( $parserOutput->getCategories() );
369 $this->addKeywords( $parserOutput );
370 $text = $parserOutput->getText();
371 wfRunHooks( 'OutputPageBeforeHTML', array( &$this, &$text ) );
372 $this->addHTML( $text );
373 $t = $parserOutput->getTitleText();
374 if( !empty( $t ) ) {
375 $this->setPageTitle( $t );
376 }
377 return true;
378 } else {
379 return false;
380 }
381 }
382
383 /**
384 * Set the maximum cache time on the Squid in seconds
385 * @param $maxage
386 */
387 function setSquidMaxage( $maxage ) {
388 $this->mSquidMaxage = $maxage;
389 }
390
391 /**
392 * Use enableClientCache(false) to force it to send nocache headers
393 * @param $state
394 */
395 function enableClientCache( $state ) {
396 return wfSetVar( $this->mEnableClientCache, $state );
397 }
398
399 function uncacheableBecauseRequestvars() {
400 global $wgRequest;
401 return $wgRequest->getText('useskin', false) === false
402 && $wgRequest->getText('uselang', false) === false;
403 }
404
405 function sendCacheControl() {
406 global $wgUseSquid, $wgUseESI, $wgSquidMaxage;
407
408 if ($this->mETag)
409 header("ETag: $this->mETag");
410
411 # don't serve compressed data to clients who can't handle it
412 # maintain different caches for logged-in users and non-logged in ones
413 header( 'Vary: Accept-Encoding, Cookie' );
414 if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) {
415 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
416 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
417 {
418 if ( $wgUseESI ) {
419 # We'll purge the proxy cache explicitly, but require end user agents
420 # to revalidate against the proxy on each visit.
421 # Surrogate-Control controls our Squid, Cache-Control downstream caches
422 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
423 # start with a shorter timeout for initial testing
424 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
425 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
426 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
427 } else {
428 # We'll purge the proxy cache for anons explicitly, but require end user agents
429 # to revalidate against the proxy on each visit.
430 # IMPORTANT! The Squid needs to replace the Cache-Control header with
431 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
432 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
433 # start with a shorter timeout for initial testing
434 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
435 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
436 }
437 } else {
438 # We do want clients to cache if they can, but they *must* check for updates
439 # on revisiting the page.
440 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
441 header( "Expires: -1" );
442 header( "Cache-Control: private, must-revalidate, max-age=0" );
443 }
444 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
445 } else {
446 wfDebug( "** no caching **\n", false );
447
448 # In general, the absence of a last modified header should be enough to prevent
449 # the client from using its cache. We send a few other things just to make sure.
450 header( 'Expires: -1' );
451 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
452 header( 'Pragma: no-cache' );
453 }
454 }
455
456 /**
457 * Finally, all the text has been munged and accumulated into
458 * the object, let's actually output it:
459 */
460 function output() {
461 global $wgUser, $wgOutputEncoding;
462 global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType, $wgProfiler;
463 global $wgJsMimeType, $wgStylePath, $wgUseAjax, $wgScriptPath, $wgServer;
464
465 if( $this->mDoNothing ){
466 return;
467 }
468 $fname = 'OutputPage::output';
469 wfProfileIn( $fname );
470 $sk = $wgUser->getSkin();
471
472 if ( $wgUseAjax ) {
473 $this->addScript( "<script type=\"{$wgJsMimeType}\">
474 var wgScriptPath=\"{$wgScriptPath}\";
475 var wgServer=\"{$wgServer}\";
476 </script>" );
477 $this->addScript( "<script type=\"{$wgJsMimeType}\" src=\"{$wgStylePath}/common/ajax.js\"></script>\n" );
478 }
479
480 if ( '' != $this->mRedirect ) {
481 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
482 # Standards require redirect URLs to be absolute
483 global $wgServer;
484 $this->mRedirect = $wgServer . $this->mRedirect;
485 }
486 if( $this->mRedirectCode == '301') {
487 if( !$wgDebugRedirects ) {
488 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
489 }
490 $this->mLastModified = wfTimestamp( TS_RFC2822 );
491 }
492
493 $this->sendCacheControl();
494
495 if( $wgDebugRedirects ) {
496 $url = htmlspecialchars( $this->mRedirect );
497 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
498 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
499 print "</body>\n</html>\n";
500 } else {
501 header( 'Location: '.$this->mRedirect );
502 }
503 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
504 wfProfileOut( $fname );
505 return;
506 }
507 elseif ( $this->mStatusCode )
508 {
509 $statusMessage = array(
510 100 => 'Continue',
511 101 => 'Switching Protocols',
512 102 => 'Processing',
513 200 => 'OK',
514 201 => 'Created',
515 202 => 'Accepted',
516 203 => 'Non-Authoritative Information',
517 204 => 'No Content',
518 205 => 'Reset Content',
519 206 => 'Partial Content',
520 207 => 'Multi-Status',
521 300 => 'Multiple Choices',
522 301 => 'Moved Permanently',
523 302 => 'Found',
524 303 => 'See Other',
525 304 => 'Not Modified',
526 305 => 'Use Proxy',
527 307 => 'Temporary Redirect',
528 400 => 'Bad Request',
529 401 => 'Unauthorized',
530 402 => 'Payment Required',
531 403 => 'Forbidden',
532 404 => 'Not Found',
533 405 => 'Method Not Allowed',
534 406 => 'Not Acceptable',
535 407 => 'Proxy Authentication Required',
536 408 => 'Request Timeout',
537 409 => 'Conflict',
538 410 => 'Gone',
539 411 => 'Length Required',
540 412 => 'Precondition Failed',
541 413 => 'Request Entity Too Large',
542 414 => 'Request-URI Too Large',
543 415 => 'Unsupported Media Type',
544 416 => 'Request Range Not Satisfiable',
545 417 => 'Expectation Failed',
546 422 => 'Unprocessable Entity',
547 423 => 'Locked',
548 424 => 'Failed Dependency',
549 500 => 'Internal Server Error',
550 501 => 'Not Implemented',
551 502 => 'Bad Gateway',
552 503 => 'Service Unavailable',
553 504 => 'Gateway Timeout',
554 505 => 'HTTP Version Not Supported',
555 507 => 'Insufficient Storage'
556 );
557
558 if ( $statusMessage[$this->mStatusCode] )
559 header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $statusMessage[$this->mStatusCode] );
560 }
561
562 # Buffer output; final headers may depend on later processing
563 ob_start();
564
565 # Disable temporary placeholders, so that the skin produces HTML
566 $sk->postParseLinkColour( false );
567
568 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
569 header( 'Content-language: '.$wgContLanguageCode );
570
571 if ($this->mArticleBodyOnly) {
572 $this->out($this->mBodytext);
573 } else {
574 wfProfileIn( 'Output-skin' );
575 $sk->outputPage( $this );
576 wfProfileOut( 'Output-skin' );
577 }
578
579 $this->sendCacheControl();
580 ob_end_flush();
581 wfProfileOut( $fname );
582 }
583
584 function out( $ins ) {
585 global $wgInputEncoding, $wgOutputEncoding, $wgContLang;
586 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
587 $outs = $ins;
588 } else {
589 $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
590 if ( false === $outs ) { $outs = $ins; }
591 }
592 print $outs;
593 }
594
595 function setEncodings() {
596 global $wgInputEncoding, $wgOutputEncoding;
597 global $wgUser, $wgContLang;
598
599 $wgInputEncoding = strtolower( $wgInputEncoding );
600
601 if( $wgUser->getOption( 'altencoding' ) ) {
602 $wgContLang->setAltEncoding();
603 return;
604 }
605
606 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
607 $wgOutputEncoding = strtolower( $wgOutputEncoding );
608 return;
609 }
610 $wgOutputEncoding = $wgInputEncoding;
611 }
612
613 /**
614 * Returns a HTML comment with the elapsed time since request.
615 * This method has no side effects.
616 * Use wfReportTime() instead.
617 * @return string
618 * @deprecated
619 */
620 function reportTime() {
621 $time = wfReportTime();
622 return $time;
623 }
624
625 /**
626 * Produce a "user is blocked" page
627 */
628 function blockedPage() {
629 global $wgUser, $wgContLang;
630
631 $this->setPageTitle( wfMsg( 'blockedtitle' ) );
632 $this->setRobotpolicy( 'noindex,nofollow' );
633 $this->setArticleRelated( false );
634
635 $id = $wgUser->blockedBy();
636 $reason = $wgUser->blockedFor();
637 $ip = wfGetIP();
638
639 if ( is_numeric( $id ) ) {
640 $name = User::whoIs( $id );
641 } else {
642 $name = $id;
643 }
644 $link = '[[' . $wgContLang->getNsText( NS_USER ) . ":{$name}|{$name}]]";
645
646 $this->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
647 $this->returnToMain( false );
648 }
649
650 /**
651 * Note: these arguments are keys into wfMsg(), not text!
652 */
653 function errorpage( $title, $msg ) {
654 global $wgTitle;
655
656 $this->mDebugtext .= 'Original title: ' .
657 $wgTitle->getPrefixedText() . "\n";
658 $this->setPageTitle( wfMsg( $title ) );
659 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
660 $this->setRobotpolicy( 'noindex,nofollow' );
661 $this->setArticleRelated( false );
662 $this->enableClientCache( false );
663 $this->mRedirect = '';
664
665 $this->mBodytext = '';
666 $this->addWikiText( wfMsg( $msg ) );
667 $this->returnToMain( false );
668
669 $this->output();
670 wfErrorExit();
671 }
672
673 /**
674 * Display an error page indicating that a given version of MediaWiki is
675 * required to use it
676 *
677 * @param mixed $version The version of MediaWiki needed to use the page
678 */
679 function versionRequired( $version ) {
680 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
681 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
682 $this->setRobotpolicy( 'noindex,nofollow' );
683 $this->setArticleRelated( false );
684 $this->mBodytext = '';
685
686 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
687 $this->returnToMain();
688 }
689
690 /**
691 * Display an error page noting that a given permission bit is required.
692 * This should generally replace the sysopRequired, developerRequired etc.
693 * @param string $permission key required
694 */
695 function permissionRequired( $permission ) {
696 global $wgUser;
697
698 $this->setPageTitle( wfMsg( 'badaccess' ) );
699 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
700 $this->setRobotpolicy( 'noindex,nofollow' );
701 $this->setArticleRelated( false );
702 $this->mBodytext = '';
703
704 $sk = $wgUser->getSkin();
705 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ) );
706 $this->addHTML( wfMsgHtml( 'badaccesstext', $ap, $permission ) );
707 $this->returnToMain();
708 }
709
710 /**
711 * @deprecated
712 */
713 function sysopRequired() {
714 global $wgUser;
715
716 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
717 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
718 $this->setRobotpolicy( 'noindex,nofollow' );
719 $this->setArticleRelated( false );
720 $this->mBodytext = '';
721
722 $sk = $wgUser->getSkin();
723 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
724 $this->addHTML( wfMsgHtml( 'sysoptext', $ap ) );
725 $this->returnToMain();
726 }
727
728 /**
729 * @deprecated
730 */
731 function developerRequired() {
732 global $wgUser;
733
734 $this->setPageTitle( wfMsg( 'developertitle' ) );
735 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
736 $this->setRobotpolicy( 'noindex,nofollow' );
737 $this->setArticleRelated( false );
738 $this->mBodytext = '';
739
740 $sk = $wgUser->getSkin();
741 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
742 $this->addHTML( wfMsgHtml( 'developertext', $ap ) );
743 $this->returnToMain();
744 }
745
746 function loginToUse() {
747 global $wgUser, $wgTitle, $wgContLang;
748
749 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
750 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
751 $this->setRobotpolicy( 'noindex,nofollow' );
752 $this->setArticleFlag( false );
753 $this->mBodytext = '';
754 $loginpage = Title::makeTitle(NS_SPECIAL, 'Userlogin');
755 $sk = $wgUser->getSkin();
756 $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'),
757 'returnto=' . htmlspecialchars($wgTitle->getPrefixedDBkey()));
758 $this->addHTML( wfMsgHtml( 'loginreqpagetext', $loginlink ) );
759
760 # We put a comment in the .html file so a Sysop can diagnose the page the
761 # user can't see.
762 $this->addHTML( "\n<!--" .
763 $wgContLang->getNsText( $wgTitle->getNamespace() ) .
764 ':' .
765 $wgTitle->getDBkey() . '-->' );
766 $this->returnToMain(); # Flip back to the main page after 10 seconds.
767 }
768
769 function databaseError( $fname, $sql, $error, $errno ) {
770 global $wgUser, $wgCommandLineMode, $wgShowSQLErrors;
771
772 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
773 $this->setRobotpolicy( 'noindex,nofollow' );
774 $this->setArticleRelated( false );
775 $this->enableClientCache( false );
776 $this->mRedirect = '';
777
778 if( !$wgShowSQLErrors ) {
779 $sql = wfMsg( 'sqlhidden' );
780 }
781
782 if ( $wgCommandLineMode ) {
783 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
784 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
785 } else {
786 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
787 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
788 }
789
790 if ( $wgCommandLineMode || !is_object( $wgUser )) {
791 print $msg."\n";
792 wfErrorExit();
793 }
794 $this->mBodytext = $msg;
795 $this->output();
796 wfErrorExit();
797 }
798
799 function readOnlyPage( $source = null, $protected = false ) {
800 global $wgUser, $wgReadOnlyFile, $wgReadOnly;
801
802 $this->setRobotpolicy( 'noindex,nofollow' );
803 $this->setArticleRelated( false );
804
805 if( $protected ) {
806 $this->setPageTitle( wfMsg( 'viewsource' ) );
807 $this->addWikiText( wfMsg( 'protectedtext' ) );
808 } else {
809 $this->setPageTitle( wfMsg( 'readonly' ) );
810 if ( $wgReadOnly ) {
811 $reason = $wgReadOnly;
812 } else {
813 $reason = file_get_contents( $wgReadOnlyFile );
814 }
815 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
816 }
817
818 if( is_string( $source ) ) {
819 if( strcmp( $source, '' ) == 0 ) {
820 global $wgTitle ;
821 if ( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
822 $source = wfMsgWeirdKey ( $wgTitle->getText() ) ;
823 } else {
824 $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
825 }
826 }
827 $rows = $wgUser->getOption( 'rows' );
828 $cols = $wgUser->getOption( 'cols' );
829 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
830 htmlspecialchars( $source ) . "\n</textarea>";
831 $this->addHTML( $text );
832 }
833
834 $this->returnToMain( false );
835 }
836
837 function fatalError( $message ) {
838 $this->setPageTitle( wfMsg( "internalerror" ) );
839 $this->setRobotpolicy( "noindex,nofollow" );
840 $this->setArticleRelated( false );
841 $this->enableClientCache( false );
842 $this->mRedirect = '';
843
844 $this->mBodytext = $message;
845 $this->output();
846 wfErrorExit();
847 }
848
849 function unexpectedValueError( $name, $val ) {
850 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
851 }
852
853 function fileCopyError( $old, $new ) {
854 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
855 }
856
857 function fileRenameError( $old, $new ) {
858 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
859 }
860
861 function fileDeleteError( $name ) {
862 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
863 }
864
865 function fileNotFoundError( $name ) {
866 $this->fatalError( wfMsg( 'filenotfound', $name ) );
867 }
868
869 /**
870 * return from error messages or notes
871 * @param $auto automatically redirect the user after 10 seconds
872 * @param $returnto page title to return to. Default is Main Page.
873 */
874 function returnToMain( $auto = true, $returnto = NULL ) {
875 global $wgUser, $wgOut, $wgRequest;
876
877 if ( $returnto == NULL ) {
878 $returnto = $wgRequest->getText( 'returnto' );
879 }
880 $returnto = htmlspecialchars( $returnto );
881
882 $sk = $wgUser->getSkin();
883 if ( '' == $returnto ) {
884 $returnto = wfMsgForContent( 'mainpage' );
885 }
886 $link = $sk->makeLinkObj( Title::newFromText( $returnto ), '' );
887
888 $r = wfMsg( 'returnto', $link );
889 if ( $auto ) {
890 $titleObj = Title::newFromText( $returnto );
891 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
892 }
893 $wgOut->addHTML( "\n<p>$r</p>\n" );
894 }
895
896 /**
897 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
898 * and uses the first 10 of them for META keywords
899 */
900 function addKeywords( &$parserOutput ) {
901 global $wgTitle;
902 $this->addKeyword( $wgTitle->getPrefixedText() );
903 $count = 1;
904 $links2d =& $parserOutput->getLinks();
905 foreach ( $links2d as $ns => $dbkeys ) {
906 foreach( $dbkeys as $dbkey => $id ) {
907 $this->addKeyword( $dbkey );
908 if ( ++$count > 10 ) {
909 break 2;
910 }
911 }
912 }
913 }
914
915 /**
916 * @access private
917 * @return string
918 */
919 function headElement() {
920 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
921 global $wgUser, $wgContLang, $wgUseTrackbacks, $wgTitle;
922
923 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
924 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
925 } else {
926 $ret = '';
927 }
928
929 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
930
931 if ( '' == $this->getHTMLTitle() ) {
932 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ));
933 }
934
935 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
936 $ret .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
937 $ret .= "<head>\n<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
938 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
939
940 $ret .= $this->getHeadLinks();
941 global $wgStylePath;
942 if( $this->isPrintable() ) {
943 $media = '';
944 } else {
945 $media = "media='print'";
946 }
947 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
948 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
949
950 $sk = $wgUser->getSkin();
951 $ret .= $sk->getHeadScripts();
952 $ret .= $this->mScripts;
953 $ret .= $sk->getUserStyles();
954
955 if ($wgUseTrackbacks && $this->isArticleRelated())
956 $ret .= $wgTitle->trackbackRDF();
957
958 $ret .= "</head>\n";
959 return $ret;
960 }
961
962 function getHeadLinks() {
963 global $wgRequest;
964 $ret = '';
965 foreach ( $this->mMetatags as $tag ) {
966 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
967 $a = 'http-equiv';
968 $tag[0] = substr( $tag[0], 5 );
969 } else {
970 $a = 'name';
971 }
972 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
973 }
974
975 $p = $this->mRobotpolicy;
976 if( $p !== '' && $p != 'index,follow' ) {
977 // http://www.robotstxt.org/wc/meta-user.html
978 // Only show if it's different from the default robots policy
979 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
980 }
981
982 if ( count( $this->mKeywords ) > 0 ) {
983 $strip = array(
984 "/<.*?>/" => '',
985 "/_/" => ' '
986 );
987 $ret .= "<meta name=\"keywords\" content=\"" .
988 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
989 }
990 foreach ( $this->mLinktags as $tag ) {
991 $ret .= '<link';
992 foreach( $tag as $attr => $val ) {
993 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
994 }
995 $ret .= " />\n";
996 }
997 if( $this->isSyndicated() ) {
998 # FIXME: centralize the mime-type and name information in Feed.php
999 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
1000 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
1001 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
1002 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 0.3' href='$link' />\n";
1003 }
1004
1005 return $ret;
1006 }
1007
1008 /**
1009 * Turn off regular page output and return an error reponse
1010 * for when rate limiting has triggered.
1011 * @todo i18n
1012 * @access public
1013 */
1014 function rateLimited() {
1015 global $wgOut;
1016 $wgOut->disable();
1017 wfHttpError( 500, 'Internal Server Error',
1018 'Sorry, the server has encountered an internal error. ' .
1019 'Please wait a moment and hit "refresh" to submit the request again.' );
1020 }
1021
1022 }
1023 ?>